<?php  
// 微信网页授权
require_once('oauth.php');
require_once('jssdk.php');
class Wegetoauth1 extends Control{
    
	public function _GET(){
		// $a = $this->sql('huodong.wx_con')->select('*')->where()->query();var_dump($a);exit;
		$authorization = new Oauth();//类的实例化
        //先检测一下是否获得了code，如果检测到code->通过code获取授权access_token->通过access_token获取用户信息->数据库查找一下授权登录用户的所有信息
		if(isset($_POST["code"])){
			//通过code获取openID，access_token,refresh_token
			$access_token_oauth2 = $authorization->oauth2_access_token($_POST["code"]);
    		$userinfo = $authorization->oauth2_get_user_info($access_token_oauth2['access_token'], $access_token_oauth2['openid']); 
    		$where = ['openid='=>$access_token_oauth2['openid']];   
    	    $list = $this->sql('huodong.wx_ceshi')->select("*")->where($where)->query();
    	    
			session_start();
		    $_SESSION['oid'] = $access_token_oauth2['openid'];
		    $_SESSION['id']  = $list[0]['id'];
		    // var_dump($_SESSION['id']);
		    //如果没有检测到用户信息，则把用户的openID(session存储起来)，headimgurl，nickname放入数组data中，然后将数据插入到ceshi表里			    
		    if (!$list) {
		                $data = [
		                 'openid'=>$userinfo['openid'],
		                 'headimgurl' =>$userinfo['headimgurl'],
		                 'nickname' => $userinfo['nickname'],
		    	        ];
		    	    	$data['create_time'] = time();
		    	    	$add = $this->sql('huodong.wx_ceshi')->insertId($data);
		    	    	$list['id']=$add;
    	    }else{
    	    //如果检测到了用户信息，更新一下授权时间添加进去
		    	    	$data['update_time'] = time();
		    	    	$updata = $this->sql('huodong.wx_ceshi')->update($data)->where($where)->query();
						// $_POST['fgid'] = 4;																										
		    	    	if (isset($_POST['fgid'])) {
		    	    		$fgid = $_POST['fgid'];
		    	    		// var_dump($fgid);exit;
		    	    	}else{
		    	    		$fgid = '';
		    	    	}
				     	
				        if($fgid != ''){
				        	  $list[0]['fgid']=$fgid;
				        	  $result1 = $this->sql('huodong.wx_con')->select('shareimg')->where(['gid='=>$fgid])->query();
					          $_SESSION['shareimg'] = $result1[0]['shareimg'];
							  					            
					            //通过gid与id的关联关系，查询wx_con表中是否存在con_openid
				        	  $id = $_SESSION['id'];
				        	  // var_dump($id);var_dump($_SESSION['oid']);
						      $con_openid = $this->sql('huodong.wx_con')->select('openid')->where(['gid='=>$fgid,'openid='=>$_SESSION['oid']])->query();
						        // var_dump($con_openid);exit;
	                            //if查询到了con_openid，通过连表查询出活动id好友的支付信息
						      if (count($con_openid) == 0 || $fgid == $_SESSION['id']) {
						        	//这是查询出所有给过钱或者没给过的记录，借助记录gid查询wx_ceshi用户信息
						            $result = $this->sql('huodong.wx_con')->select('*')->where(['gid='=>$fgid])->query();
						            // var_dump($result);
						            //将分享人的图片和总钱数显示出来
						            $image = $_SESSION['shareimg'];
						            $money = 0;
						            // $data['data'] = $result;
						            foreach ($result as $key => $value) {
						            	//根据查询记录gid查询用户信息
						            	$data1 = $this->sql('huodong.wx_ceshi')->select('*')->where(['openid='=>$value['openid']])->query();
						            	$result[$key]['headimgurl'] = $data1[0]['headimgurl'];
						            	$result[$key]['nickname']= $data1[0]['nickname'];
						            	if (isset($result[$key]['isgive'])&&($result[$key]['isgive'] == 1)) {
						            		$money += $result[$key]['gmoney'];	
						            	}
						            	
						            }
						            //这是总钱数
						            $data2['money'] = $money;
						            //分享图片
						            $data2['image'] = $image;
						            //这其中包含用户头像以及给的钱数
						            $data2['data'] = $result;
						            $data2['fgid'] = $fgid;//将fgid添加到数组上
						            // var_dump($data['data']);exit();
						            $this->display('ispayshow',$data2);
						       }else{
						        	$list[0]['fgid']=$fgid;
						        	// var_dump($list[0]['fgid']);exit();
						            $this->display('ispay',$list[0]);
						       }
					    }else{
                                $aro = new jssdk();
							    $access_token = $aro->getAccessToken();
							    $data = $aro->GetSignPackage();
							    $id = $_SESSION['id'];
							    $image = $this->sql('huodong.wx_ceshi')->select('*')->where(['id ='=>$id])->query();
							    if ($image) {
							       $data['image'] = $image[0]['shareimg'];
							       $gid = $_SESSION['id'];
							       $data['gid'] = $gid;  
							    }else{
							       $data['image'] = '';
							    }
							    $this->display('sign',$data);
					    }
	    	}    
		}
		else{
			//若没有获取code，则重新获取code
			$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    		$jumpurl = $authorization->oauthUrl($redirect_url, "snsapi_userinfo", "456");
    		Header("Location: $jumpurl");
		}	
	}
}
?>
